package com.ruoyi.lm.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.lm.domain.LmMessage;
import com.ruoyi.lm.service.ILmMessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmUser;
import com.ruoyi.lm.service.ILmUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/user")
public class LmUserController extends BaseController
{
    private String prefix = "lm/user";

    @Autowired
    private ILmUserService lmUserService;
    @Autowired
    private ILmMessageService lmMessageService;

    @RequiresPermissions("lm:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("lm:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmUser lmUser)
    {
        startPage();
        List<LmUser> list = lmUserService.selectLmUserList(lmUser);
        return getDataTable(list);
    }

    /**
     * 查询好友列表
     */
    @RequiresPermissions("lm:user:list")
    @PostMapping("/listInvention")
    @ResponseBody
    public TableDataInfo listInvention(LmUser lmUser)
    {
        startPage();
        List<LmUser> list = lmUserService.selectLmUserList(lmUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("lm:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmUser lmUser)
    {
        List<LmUser> list = lmUserService.selectLmUserList(lmUser);
        ExcelUtil<LmUser> util = new ExcelUtil<LmUser>(LmUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("lm:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmUser lmUser)
    {
        return toAjax(lmUserService.insertLmUser(lmUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmUser lmUser = lmUserService.selectLmUserById(id);
        mmap.put("lmUser", lmUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmUser lmUser)
    {
        return toAjax(lmUserService.updateLmUser(lmUser));
    }

    /**
     * 审批服务商
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUser")
    @ResponseBody
    public AjaxResult reviewUser(String id)
    {
        LmUser lmUser = lmUserService.selectLmUserById(id);
        LmUser temp = new LmUser();
        temp.setId(id);
        if("1".equals(lmUser.getIsQiye())){
            //企业认证
            temp.setIsServiceuser("2");
            temp.setReviewStatus("5");
            temp.setIsQiye("2");
        }else if("1".equals(lmUser.getIsGeren())){
            //个人认证
            temp.setIsServiceuser("1");
            temp.setReviewStatus("2");
            temp.setIsGeren("2");
        }
        int num = lmUserService.updateLmUser(temp);
        if(num >0){
            //添加通过消息提醒
            LmMessage lmMessage =new LmMessage();
            lmMessage.setMessageUser(lmUser.getId());
            lmMessage.setMessageTitle("服务商认证通过");
            lmMessage.setMessageContent("恭喜您，认证已通过");
            lmMessage.setMessageType("个人");
            lmMessageService.insertLmMessage(lmMessage);
        }
        return toAjax(num);
    }


    /**
     * 用户封号
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户封号", businessType = BusinessType.UPDATE)
    @PostMapping("/disableUser")
    @ResponseBody
    public AjaxResult disableUser(String id)
    {
        LmUser temp = new LmUser();
        temp.setId(id);
        temp.setIsValidate("是");
        return toAjax(lmUserService.updateLmUser(temp));
    }

    /**
     * 用户解封
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户解封", businessType = BusinessType.UPDATE)
    @PostMapping("/enableUser")
    @ResponseBody
    public AjaxResult enableUser(String id)
    {
        LmUser temp = new LmUser();
        temp.setId(id);
        temp.setIsValidate("否");
        return toAjax(lmUserService.updateLmUser(temp));
    }

    /**
     * 查看用户
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        LmUser lmUser = lmUserService.selectLmUserById(id);
        mmap.put("lmUser", lmUser);
        return prefix + "/view";
    }

    /**
     * 拒绝服务商
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUserDis")
    @ResponseBody
    public AjaxResult reviewUserDis(LmUser lmUser)
    {
        LmUser lmUser1 = lmUserService.selectLmUserById(lmUser.getId());
        LmUser temp = new LmUser();
        temp.setId(lmUser.getId());
        if("1".equals(lmUser1.getIsQiye())){
            //企业认证
            temp.setReviewStatus("4");
            temp.setIsQiye("3");
        }else if("1".equals(lmUser1.getIsGeren())){
            //个人认证
            temp.setReviewStatus("1");
            temp.setIsGeren("3");
        }
        temp.setIsServiceuser("0");
       /* temp.setUserImg("");
        temp.setUserImgback("");
        temp.setUserLicense("");*/
        temp.setDisReason(lmUser.getDisReason());
        int num = lmUserService.updateLmUser(temp);
        if(num >0){
            //添加拒绝消息提醒
            LmMessage lmMessage =new LmMessage();
            lmMessage.setMessageUser(lmUser.getId());
            lmMessage.setMessageTitle("服务商认证拒绝");
            lmMessage.setMessageContent(lmUser.getDisReason());
            lmMessage.setMessageType("个人");
            lmMessageService.insertLmMessage(lmMessage);
        }
        return toAjax(num);
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("lm:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmUserService.deleteLmUserByIds(ids));
    }
}
